import re
import uuid

from mongoengine import QuerySet, Q

def select_fund(queryset, id=None, info=None, org=None):
    """
    根据条件查询，用于搜索框查询，搜索框有两个字段：关键词(多个字段共用一个搜索框)和开展机构(单位org)
    """
    if not (id or info or org):
        return None
    result = queryset
    if id is not None:
        result = result.filter(id=id)
    if org is not None:
        result = result.filter(org__icontains=org)
    if info is not None:
        # result = result(Q(pic__icontains=info) | Q(type__icontains=info) | Q(department__icontains=info) | Q(project__icontains=info) |
        #                 Q(subjectClass__first_level__icontains=info) | Q(subjectClass__second_level__icontains=info) | Q(subjectClass__third_level__icontains=info))
        # 只在题目中进行查询
        result = result(project__icontains=info)
    return result

def select_EU(queryset, id=None, info=None, coordinator=None):
    if not (id or info or coordinator):
        return None
    result = queryset
    if id is not None:
        result = result.filter(id=id)
    if coordinator is not None:
        result = result.filter(coordinator__icontains=coordinator)
    if info is not None:
        re_part = '.*' + info + '.*'
        # result = result(Q(title__icontains=info) | Q(objective__icontains=info) | Q(participants=re.compile(re_part, re.IGNORECASE)))
        result = result(title__icontains=info)
    return result


def select_USA(queryset, id=None, info=None, unit=None):
    if not (id or info or unit):
        return None
    result = queryset
    if id is not None:
        result = result.filter(id=id)
    if info is not None:
        # result = result(Q(title__icontains=info) | Q(abstract__icontains=info) | Q(coordinator__icontains=info) | Q(participants__icontains=info))
        result = result(title__icontains=info)
    if unit is not None:
        result = result.filter(unit__icontains=unit)
    return result

